﻿(*
    The MIT License

    Copyright (c)2010 Fondevila Ltd

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
*)


namespace uk.co.fondevila.GoGo.Reader

open System
open System.Collections.Generic
open System.ServiceModel.Syndication

open uk.co.fondevila.GoGo.Base


type public ReaderItem (item: SyndicationItem) =
    inherit GoGoSyndicationItem(item)

    let mutable _Id = ""
    let mutable _Author = ""
    let mutable _Url = null
    let mutable _Title = ""
    let mutable _Summary = ""
    let mutable _Feed = null
    let mutable _UpdateTime = DateTimeOffset()
    let mutable _PublishDate = DateTimeOffset()
    let mutable _Tags = null
    let mutable _States = null
    do
        _Tags <- new List<string>()
        _States <- new List<State>()

        _Id <- item.Id
        _PublishDate <- item.PublishDate
        _UpdateTime <- item.LastUpdatedTime
        _Title <- base.getTextSyndicationContent(item.Title)

        _Summary <- base.getTextSyndicationContent(item.Summary)
        if System.String.IsNullOrEmpty(_Summary) then
            _Summary <- base.getTextSyndicationContent(item.Content)

        if item.Authors.Count > 0 then
            _Author <- item.Authors.[0].Name
        if item.Links.Count > 0 then
            _Url <- item.Links.[0].Uri

        item.Categories 
        |> Seq.iter (fun cat ->
                        let state = StateFormatter.ToState cat.Label
                        if state <> State.Label && state <> State.Unknown then
                            _States.Add(state)
                        if cat.Name.EndsWith("/label/" + cat.Label) then
                            _Tags.Add(cat.Label) )

        _Feed <- new Feed(item)


    member public r.Id
        with get() = _Id
        and set(value) = _Id <- value

    member r.Author
        with public get() = _Author
        and private set(value) = _Author <- value

    member r.Url
        with public get() = _Url
        and private set(value) = _Url <- value

    member r.Title
        with public get() = _Title
        and private set(value) = _Title <- value

    member r.Summary
        with public get() = _Summary
        and private set(value) = _Summary <- value

    member r.Feed
        with public get() = _Feed
        and private set(value) = _Feed <- value

    member r.UpdateTime
        with public get() = _UpdateTime
        and private set(value) = _UpdateTime <- value

    member r.PublishDate
        with public get() = _PublishDate
        and private set(value) = _PublishDate <- value

    member r.Tags
        with public get() = _Tags
        and private set(value) = _Tags <- value

    member r.States
        with public get() = _States
        and private set(value) = _States <- value



